package org.NooLab.structures.nlp;

import java.io.Serializable;
import java.util.ArrayList;






public class WordSet implements Serializable{
	
	private static final long serialVersionUID = -8813057492829172835L;
	
	String guid = ""; 
	String parentGuid = "";
	
	
	ArrayList<NlpWord> words = new ArrayList<NlpWord> ();
	private boolean removalAllowed;
	private boolean consecutiveWords;
	
	
	// ------------------------------------------------------------------------
	public WordSet(){
		
	}
	// ------------------------------------------------------------------------

	public int size(){
		return words.size();
	}

	public boolean isEqual(WordSet compWordset) {
		boolean rB=true;
		NlpWord w1,w2;
		
		ArrayList<String> cLabels, wLabels,wTags,cTags;
		
		
		if (words.size() != compWordset.size()){
			return false;
		}
		int k = Math.min(words.size(), compWordset.size());
	
		wLabels = getLabels();
		cLabels = compWordset.getLabels();

		wTags = getPoSTags();
		cTags = compWordset.getPoSTags() ;

		
		for (int i=0;i<wLabels.size(); i++){
			boolean hb = cLabels.contains( wLabels.get(i)); 
			if (hb){
				hb = cTags.contains( wTags.get(i));
			}
			
			if (hb==false){
				rB=false;
				break;
			}
		}// i->
		
		return rB;
	}
	
	
	
	
	public void makeEntriesUnique() {
		// 
		NlpWord w_i,w_k ;
		
		for (int i=0;i<words.size();i++){

			w_i = words.get(i);
			for (int k=words.size()-1;k>i;k--){
			
				w_k = words.get(k);
				if (w_i.equals(w_k)){
					words.remove(k);
				}
				
			}// k->0
			
		}// i-> ...
		
		
	}
	public int removeWordByTag(String[] synTags) {
		// 
		NlpWord nword ;
		String pTag , sTag;
		
		
		for (int i=words.size()-1;i>=0;i--){
			nword = words.get(i);
			pTag = nword.PosTag;
			for (int t=0;t<synTags.length;t++){
				
				sTag = synTags[t] ; 
				int wp = sTag.indexOf("*") ;
				sTag = sTag.replace("*", "");
				
				if (pTag.contains(sTag)){
					words.remove(i);
				}
			}// t->
			
		} // i->
		
		return size();
	}
	
	
	public void integrateWordSet(WordSet inWset) {
		// 
		NlpWord nword ;
		
		for (int i=0;i<inWset.size();i++){
			nword = inWset.getWord(i) ;
			// actually, we could improve by considering nlpPositions...
			words.add(nword);
		}
		
	}

	public ArrayList<String> getLabels() {
		ArrayList<String> labels = new ArrayList<String>();
		
		for (int i=0;i<words.size();i++){
			labels.add( words.get(i).label );	
		}
		
		return labels;
	}

	public ArrayList<String> getPoSTags() {
		ArrayList<String> tags = new ArrayList<String>();
		
		for (int i=0;i<words.size();i++){
			tags.add( words.get(i).PosTag );	
		}
		
		return tags;
	}


	public String getGuid() {
		return guid;
	}


	public void setGuid(String guid) {
		this.guid = guid;
	}


	public String getParentGuid() {
		return parentGuid;
	}


	public void setParentGuid(String parentGuid) {
		this.parentGuid = parentGuid;
	}

	public NlpWord getLastWord( ) {
		return words.get(words.size()-1);
	}

	public NlpWord getWord( int index) {
		NlpWord nw = null;
		
		if ((index>=0) && (index<words.size())){
			nw = words.get(index);
		}
		return nw ;
	}

	public ArrayList<NlpWord> getWords() {
		return words;
	}


	public void setWords(ArrayList<NlpWord> words) {
		this.words = words;
	}

	public void setRemovalAllowed(boolean flag) {
		// 
		removalAllowed = flag;
	}

	public boolean isRemovalAllowed() {
		return removalAllowed;
	}

	public void setConsecutiveWords(boolean flag) {
		consecutiveWords = true;
	}
	public boolean isConsecutiveWords() {
		return consecutiveWords ;
	}
	

}
